<?php
include_once "../../qc_base.php";
include_once "../../qc_utils.php";
include_once "../../bootstrap.php";

include_once "../../classes/MakePage.php";


/**
   * This file contains the class
   * @package phpnewmaker
   */
   


function makesuredir($dir){
	if(!file_exists(dirname($dir))){
		makesuredir(dirname($dir));
	}
	if(!file_exists($dir)){
		mkdir($dir);
	}
}


function write_pdost_form_to_file($crudset_id){

	$qcs = Doctrine_Query::create()
	->from('Crudsets cs')->where('crudset_id = ?',$crudset_id);
	$setinfo = $qcs->fetchOne();


	// get the main table for the set
	$qtb = Doctrine_Query::create()
	->from('Tablesources tb')->where('tablesource_id = ?',$setinfo['main_table']);
	$tablesource = $qtb->fetchOne();
	$table_name = $tablesource['table_name'];







	$q = Doctrine_Query::create()
	->from('Projects p')->where('project_id = ?',$setinfo['project_id']);
	$project = $q->execute()->getFirst();	
	
	$project_db = $project->project_db;
	$project_path = $project->project_path;
	$project_server = $project->project_server;
	$project_login = $project->project_login;
	$project_pass = $project->project_pass;
   	$project_name = $project->project_name;
	$class_subpath = $project->class_subpath;
	$template_dir = $project->template_dir;	
	$structure = $project->file_structure;




	$structure_path = QC_SYS_BASEDIR . '/structures/';
	
	include $structure_path . $structure .'/paths.php';
	

	$outpath = $paths['modules'];
	$outpath = str_replace('<setname>',$setinfo['cs_name'],$outpath);
	
	$outpath = QC_SYS_BASEDIR . '/' . QC_PROJECT_SUBPATH . $project_name . '/' . $outpath ;

	makesuredir($outpath);
	
	
	$targetfile = $outpath . $setinfo['cs_name'] . '_form.php';
	$tplfile = 'pdost_form_tpl.php';

//--------------------Create Replacement Text

$setcsname = '
$name = \'' . $setinfo['cs_name'] . '\'; 
';

$fieldspec = '
$fieldspec = &$' . $setinfo['cs_name'] . '_spec;
';

$setquery = '
		$query = \'SELECT * FROM ' . $table_name . ' WHERE 1 \';
';

$Lookup = '
$'. $setinfo['cs_name'] . 'Lookups = new ' . $setinfo['cs_name'] . 'LookupList();
$'. $setinfo['cs_name'] . 'Lookups->SetFilter($Filters);
$'. $setinfo['cs_name'] . 'Lookups->ResolveSpecLists($fieldspec);

';

// select control class from edit type
$ar_controltype['text']='CtlText';
$ar_controltype['querylist']='CtlSelect';
$ar_controltype['commalist']='CtlSelect';
$ar_controltype['checkbox']='CtlCheckbox';
$ar_controltype['calendar']='CtlJQDate';
$ar_controltype['textarea']='CtlTextArea';
// Create control for all columns that have edit_type = 'querylist'
	$q = Doctrine_Query::create()
	->from('Crudcols cc')->where('crudset_id = ?',$crudset_id);
	$list_array = $q->fetchArray();
	
	foreach ($list_array as $rowkey => $currentrow){
		$variable_id = $currentrow['variable_id'];
		//print 'Getting Variable for id ' . $currentrow['variable_id'];
		$qv = Doctrine_Query::create()
		->from('Variables v')->where('variable_id = ?',$variable_id);
		$varsrecord = $qv->fetchOne();
		$variable_name = $varsrecord['variable_name'];	

print 'Checking column ' . $variable_name . " num {$currentrow['crudcol_id']}Is " . $currentrow['edit_type'] . "\n";
		$ctls .= '
		<label for="' . $variable_name . '" >' . $variable_name . '</label>
		<?php 
		if(!isset($filter_row[\'' . $variable_name . '\'])){
		print \'<li>\' . MakePhpCtl($ar_SelToCtl[$fieldspec[\'' . $variable_name . '\'][\'select\']],$spec, $result_row[\'' . $variable_name . '\'],$fieldspec[\'' . $variable_name . '\'][\'list\']) . \'</li>\'; 
		}
		?>
		';

	}
	
// Create hidden controls for keys	
	
//---------------------------Write into file	
	
	

	$pagemaker = new MakePage();

	$pagemaker->AddReplacement('setcsname',$setcsname);	
	$pagemaker->AddReplacement('fieldspec',$fieldspec);
	$pagemaker->AddReplacement('setquery',$setquery);
	$pagemaker->AddReplacement('Lookup',$Lookup);
	$pagemaker->AddReplacement('ctls',$ctls);	
	
	$newstuff = $pagemaker->ReplaceInFile($tplfile,$targetfile);
	
	$pagemaker->PrintReplacementsTable();

	echo '<pre>' . $pagemaker->str_log . '</pre>';
	
	file_put_contents($targetfile,$newstuff);	

	return $newstuff;


}
?>
